import json

from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from axios_orm_king.models import User, Heros


def admin(request):
    return render(request, 'login.html')


class System(View):
    def get(self, request):
        return render(request, 'heros.html')

    def post(self, request):
        input_data = json.loads(request.body.decode())
        input_id = int(input_data['input_id'])
        input_pwd = int(input_data['input_pwd'])
        try:
            user = User.objects.get(user_id=input_id)
        except Exception:
            return JsonResponse({'code': 999, 'message': '用户不存在'})
        if user.user_password != input_pwd:
            return JsonResponse({'code': 999, 'message': '密码输入错误'})
        else:
            response = JsonResponse({'code': 0, 'message': '登陆成功'})
            response.set_cookie('user_id', user.id)
            response.set_cookie('user_pwd', user.user_password)
            return response


def get_heros(request):
    hero_qdict = Heros.objects.all()
    print(hero_qdict)
    hero_list = []
    for hero in hero_qdict:
        hero_dict = {
            'name': hero.name,
            'hp_max': hero.hp_max,
            'hp_growth': hero.hp_growth,
            'hp_start': hero.hp_start,
            'mp_max': hero.mp_max,
            'mp_growth': hero.mp_growth,
            'mp_start': hero.mp_start,
            'attack_max': hero.attack_max,
            'attack_growth': hero.attack_growth,
            'attack_start': hero.attack_start,
            'defense_max': hero.defense_max,
            'defense_growth': hero.defense_growth,
            'defense_start': hero.defense_start,
            'hp_5s_max': hero.hp_5s_max,
            'hp_5s_growth': hero.hp_5s_growth,
            'hp_5s_start': hero.hp_5s_start,
            'mp_5s_max': hero.mp_5s_max,
            'mp_5s_growth': hero.mp_5s_growth,
            'mp_5s_start': hero.mp_5s_start,
            'attack_speed_max': hero.attack_speed_max,
            'attack_range': hero.attack_range,
            'role_main': hero.role_main,
            'role_assist': hero.role_assist,
            'birthdate': hero.birthdate,
        }
        hero_list.append(hero_dict)

    return JsonResponse({'code': 0, 'message': '成功读取英雄数据', 'data': hero_list})


def heros(request):
    return render(request, 'heros.html')
